using System;
using System.Collections.Generic;
using System.Text;
namespace AlipayClass
{
	public class AlipayService
	{
		private string gateway = "";
		private string _key = "";
		private string _input_charset = "";
		private string _sign_type = "";
		private string mysign = "";
		private Dictionary<string, string> sPara = new Dictionary<string, string>();
		public AlipayService(string partner, string seller_email, string return_url, string notify_url, string show_url, string out_trade_no, string subject, string body, string total_fee, string paymethod, string defaultbank, string encrypt_key, string exter_invoke_ip, string extra_common_param, string buyer_email, string royalty_type, string royalty_parameters, string it_b_pay, string key, string input_charset, string sign_type)
		{
			this.gateway = "https://www.alipay.com/cooperate/gateway.do?";
			this._key = key.Trim();
			this._input_charset = input_charset.ToLower();
			this._sign_type = sign_type.ToUpper();
			this.sPara = AlipayFunction.Para_filter(new SortedDictionary<string, string>
			{

				{
					"service",
					"create_direct_pay_by_user"
				},

				{
					"payment_type",
					"1"
				},

				{
					"partner",
					partner
				},

				{
					"seller_email",
					seller_email
				},

				{
					"return_url",
					return_url
				},

				{
					"notify_url",
					notify_url
				},

				{
					"_input_charset",
					this._input_charset
				},

				{
					"show_url",
					show_url
				},

				{
					"out_trade_no",
					out_trade_no
				},

				{
					"subject",
					subject
				},

				{
					"body",
					body
				},

				{
					"total_fee",
					total_fee
				},

				{
					"paymethod",
					paymethod
				},

				{
					"defaultbank",
					defaultbank
				},

				{
					"anti_phishing_key",
					encrypt_key
				},

				{
					"exter_invoke_ip",
					exter_invoke_ip
				},

				{
					"extra_common_param",
					extra_common_param
				},

				{
					"buyer_email",
					buyer_email
				},

				{
					"royalty_type",
					royalty_type
				},

				{
					"royalty_parameters",
					royalty_parameters
				},

				{
					"it_b_pay",
					it_b_pay
				}
			});
			this.mysign = AlipayFunction.Build_mysign(this.sPara, this._key, this._sign_type, this._input_charset);
		}
		public string Build_Form()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(string.Concat(new string[]
			{
				"<form id=\"alipaysubmit\" target=\"_blank\"  name=\"alipaysubmit\" action=\"",
				this.gateway,
				"_input_charset=",
				this._input_charset,
				"\" method=\"post\">"
			}));
			foreach (KeyValuePair<string, string> current in this.sPara)
			{
				stringBuilder.Append(string.Concat(new string[]
				{
					"<input type=\"hidden\" name=\"",
					current.Key,
					"\" value=\"",
					current.Value,
					"\"/>"
				}));
			}
			stringBuilder.Append("<input type=\"hidden\" name=\"sign\" value=\"" + this.mysign + "\"/>");
			stringBuilder.Append("<input type=\"hidden\" name=\"sign_type\" value=\"" + this._sign_type + "\"/>");
			stringBuilder.Append("<input style=\"display:none;\" type=\"submit\" value=\"支付宝确认付款\"></form>");
			stringBuilder.Append("<script>document.forms['alipaysubmit'].submit();</script>");
			return stringBuilder.ToString();
		}
	}
}
